package com.dhecp.project.system.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.system.domain.SysButtonState;
import com.dhecp.project.system.mapper.SysButtonStateMapper;
import com.dhecp.project.system.service.SysButtonStateService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 按钮状态 服务实现层
 * @author DH-lwl
 * @version 2020年4月2日
 */
@Service
public class SysButtonStateServiceImpl implements SysButtonStateService {

	@Autowired
	private SysButtonStateMapper buttonStateMapper;

	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;
	
	/**
	 * 查询所有按钮状态
	 * @param params
	 * @return List-SysButtonState
	 * @author lwl
	 * @throws Exception 
	 */
	@Override
	public List<SysButtonState> queryButtonState(Map<String, Object> params) throws Exception {
		params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));
		PageUtils.startPage();
		return buttonStateMapper.findButtonState(params);
	}
	
	/**
	 * 通过guid查询单条记录
	 * @param guid
	 * @return SysButtonState
	 * @author lwl
	 */
	@Override
	public SysButtonState queryButtonStateByGuid(String guid) {
		return buttonStateMapper.findButtonStateByGuid(guid);
	}
	
	/**
	 * 新增按钮状态信息
	 * @param buttonStateItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	public AjaxResult insertButtonState(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<SysButtonState> buttonStateList = JSON.parseArray(params.get("buttonStateItems").toString(), SysButtonState.class);
		
		if (buttonStateList == null || buttonStateList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysButtonState buttonState : buttonStateList) {
			buttonStateMapper.insertButtonState(buttonState);
		}
		return AjaxResult.success("操作成功");
	}

	/**
	 * 删除按钮状态信息
	 * @param buttonStateItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	public AjaxResult deleteButtonState(Map<String, Object> params) {
		List<SysButtonState> buttonStateList = JSON.parseArray(params.get("buttonStateItems").toString(), SysButtonState.class);
		if (buttonStateList == null || buttonStateList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysButtonState buttonState : buttonStateList) {
			buttonStateMapper.deleteButtonState(buttonState);
		}
		return AjaxResult.success("操作成功");
	}

	/**
	 * 通过guid修改按钮状态信息
	 * @param buttonStateItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	public AjaxResult updateButtonStateByGuid(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<SysButtonState> buttonStateList = JSON.parseArray(params.get("buttonStateItems").toString(), SysButtonState.class);
		if (buttonStateList == null || buttonStateList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysButtonState buttonState : buttonStateList) {
			buttonStateMapper.updateByGuid(buttonState);
		}
		return AjaxResult.success("操作成功");
	}

}
